package com.imut.oa.controller.common;


import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;

import com.imut.oa.common.BaseResponse;
import com.imut.oa.common.ResultUtils;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import static com.imut.oa.config.CaptureConfiguration.CAPTCHA_MAP;

@CrossOrigin
@RestController
@RequestMapping
@Slf4j
@Tag(name = "验证码")
public class CaptureController {
    @PostMapping("/captcha")
    @Operation(summary = "获取验证码")
    public BaseResponse captcha(@RequestParam String key, HttpServletRequest request, HttpServletResponse response) throws Exception{
        //算术类型
        LineCaptcha captcha = CaptchaUtil.createLineCaptcha(140, 40, 4,70); // 宽度、高度、字符和干扰线数量可自定义

        // 设置验证码内容和输出到http响应
        String code = captcha.getCode(); // 获取验证码文本内容
//        captcha.write("D:\\YRX\\project\\oa\\oa\\src\\main\\java\\com\\imut\\oa\\pic\\a.png");
//        captcha.write(response.getOutputStream()); // 输出图片到浏览器

        //将验证码内容存入响应头中
        log.info("key：{}",key);
        log.info("code：{}",code);
        CAPTCHA_MAP.put(key, code);
        return ResultUtils.success(captcha.getImageBase64Data());
    }
}
